<template>
  <div class="item-list">
    <ul>
      <li class="item"
          v-for="item  of letters"
          :key="item"
          :ref="item"
          @touchstart="HandTouchStart"
          @touchmove="HandTouchMove"
          @touchend="HandTouchEnd"
          @click="handLetterClick">{{item}}</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'CityAlphabet',
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  props: {
    cities: Object
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    handLetterClick (e) {
      this.$emit('change', e.target.innerText)
      //console.log(e.target.innerText)
    },
    HandTouchStart () {
      this.touchStatus = true
    },
    HandTouchMove (e) {
      if (this.touchStatus) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY
          const index = Math.floor((touchY - (this.startY + 95)) / 20)
          if (index >= 0 && index <= this.letters.length) {
            this.$emit('change', this.letters[index])
          }
        }, 16)
      }
    },
    HandTouchEnd () {
      this.touchStatus = false
    }
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import "~style/varibles.styl"
  .item-list
    display flex
    flex-direction column
    justify-content center
    position absolute
    right 0
    top 2rem
    bottom 2rem
    width .4rem
    color  $bgColor
    .item
      line-height .4rem
      text-align center
</style>
